package com.suke.czx.modules.info.controller;

import com.suke.czx.common.annotation.SysLog;
import com.suke.czx.common.base.AbstractController;
import com.suke.czx.common.utils.ParamValidateUtil;
import com.suke.czx.common.utils.R;
import com.suke.czx.common.validate.InfoSaveGroup;
import com.suke.czx.common.validate.InfoUpdateGroup;
import com.suke.czx.modules.info.entity.Info;
import com.suke.czx.modules.info.entity.InfoSearchBo;
import com.suke.czx.modules.info.service.InfoService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 应用模块名称: 信息
 * 代码描述: 信息控制层
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/01 22:45
 */
@Validated
@RestController
@RequestMapping("/api/info")
@AllArgsConstructor
@Api(value = "InfoController", tags = "信息管理")
public class InfoController extends AbstractController {

    private final InfoService infoService;

    /**
     * 分页查询信息列表
     * @param infoSearchBo 查询BO
     * @return VO
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:info:list')")
    public R list(InfoSearchBo infoSearchBo) {
        if (infoSearchBo.getPage() == null || infoSearchBo.getLimit() == null) {
            return R.error("缺少分页信息");
        }
        // 分页查询信息列表
        return infoService.pageInfoList(infoSearchBo);
    }

    /**
     * 保存信息
     * @param info 信息
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("保存信息")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:info:save')")
    public R save(@Validated(InfoSaveGroup.class) Info info, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        info.setCreator(getUserId());
        info.setCreateTime(new Date());
        infoService.save(info);
        return R.ok();
    }

    /**
     * 更新信息
     * @param info 信息
     * @param bindingResult bindingResult
     * @return VO
     */
    @SysLog("更新信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:info:update')")
    public R update(@Validated(InfoUpdateGroup.class) Info info, BindingResult bindingResult) {
        // 入参校验结果
        if (bindingResult.hasErrors()) {
            return R.error(ParamValidateUtil.paramVilated(bindingResult));
        }
        info.setUpdater(getUserId());
        info.setUpdateTime(new Date());
        infoService.updateById(info);
        return R.ok();
    }

    /**
     * 删除信息
     * @param infoId 信息编码
     * @return VO
     */
    @SysLog("删除信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @PreAuthorize("hasRole('but:info:delete')")
    public R delete(@NotNull(message = "信息编码不能为空") Long infoId) {
        return infoService.updateDelInfo(infoId, getUserId());
    }

}
